package com.baizhi.controller;


import com.baizhi.entity.Brand;
import com.baizhi.entity.Category;
import com.baizhi.entity.CategoryBrandRelation;
import com.baizhi.service.IBrandService;
import com.baizhi.service.ICategoryBrandRelationService;
import com.baizhi.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author longyang
 * @since 2022-01-19
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private IBrandService brandService;
    @Autowired
    private ICategoryBrandRelationService iconBrandRelationService;;
    //根据类别的编号查询该类别下面的品牌信息，分页查询
    @GetMapping("selectBrandByCategoryId/{categoryId}")
    public ResultEntity<PageInfo<Brand>> selectChildrenCategory(@PathVariable("categoryId")Integer categoryId,int pageNo,int pageSize){
        PageInfo<Brand> brandPageInfo = brandService.selectBrandByCategoryId(categoryId, pageNo, pageSize);
        if (brandPageInfo!=null){
            //成功
            return ResultEntity.ok(brandPageInfo);
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
    //添加品牌
    @PostMapping("add")
    public ResultEntity<String> addBrand(Brand brand){
        Integer integer = brandService.addBrand(brand);
        if (integer==1){
            //成功
            return ResultEntity.ok();
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
    //品牌图片上传
    @Value("${imgePath}")
    private String imge;
    @PostMapping("uploadImg")
    public ResultEntity<String> uploadImg(MultipartFile brandImg) throws IOException {
        String oldName = brandImg.getOriginalFilename();
        System.err.println(oldName);
        String newName = UUID.randomUUID().toString() + oldName;
        System.err.println(newName);
        File file = new File(imge+newName);
        brandImg.transferTo(file);//把文件保存起来
        if (newName!=null){
            //成功
            return ResultEntity.ok(imge+newName);
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
    //删除品牌
    @DeleteMapping("delete/{brandId}")
    public ResultEntity<String> deleteBrand(@PathVariable("brandId") Integer id){
        Integer integer = brandService.deleteBrand(id);
        if (integer==1){
            return ResultEntity.ok();
        }else {
            return ResultEntity.error();
        }
    }
    //根据品牌id查询品牌信息
    @GetMapping("selectOne/{brandId}")
    public ResultEntity<Brand> selectBrandbyId(@PathVariable("brandId")Integer brandId){
        Brand brand = brandService.selectBrandById(brandId);
        if (brand!=null){
            //成功
            return ResultEntity.ok(brand);
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
    //修改品牌
    @PutMapping("update")
    public ResultEntity<String> updateBrand(Brand brand){
        Integer integer = brandService.updateBrand(brand);
        if (integer==1){
            //成功
            return ResultEntity.ok();
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
    //给品牌关联商品类别
    @PostMapping("relaCate")
    public ResultEntity<String> addCategoryBrand(Integer brandId,int[] cateIds){
        //测试结果是否为空
        System.err.println(brandId);
        System.err.println(cateIds);
        //初始化返回结果为0
        Integer integer=0;
        //创建CategoryBrandRelation原始对象
        CategoryBrandRelation Cbr = new CategoryBrandRelation();
        //遍历拿到的数组
        for(int i=0;i<cateIds.length;i++){
            //往对象里面添加数据
            Cbr.setBrandId(brandId);
            Cbr.setCategoryId(cateIds[i]);
            //调用分配方法
            integer = iconBrandRelationService.addCategoryBrand(Cbr);
        }
        //判断返回数据
        if (integer==1){
            //成功
            return ResultEntity.ok();
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
}

